package org.hive.vmware.repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hive.vmware.model.VmCenterServer;
import org.hive.vmware.model.VmGroupBee;
import org.hive.vmware.persistence.DiskPersistence;
import org.hive.vmware.translator.VmCenterServerModelDiskPersistenceTranslator;
import org.hive.vmware.translator.VmGroupModelDiskPersistenceTranslator;

public class VmwareRepository {
	
	VmCenterServerModelDiskPersistenceTranslator vmCenterPersistenceTranslator = new VmCenterServerModelDiskPersistenceTranslator();
	VmGroupModelDiskPersistenceTranslator vmGroupDiskPersistenceTranslator = new VmGroupModelDiskPersistenceTranslator();
	DiskPersistence diskPersistence = new DiskPersistence();
	
	public List<VmCenterServer> findAllVmCenterServers() {
		ArrayList<VmCenterServer> centers = new ArrayList<VmCenterServer>();
		
		try {
			List<String> servers = diskPersistence.loadVmwareCenterServers();
			for (String string : servers) {
				VmCenterServer server = vmCenterPersistenceTranslator.translateVmCenterServerToModel(string);
				if (server != null) {
					centers.add(server);
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return centers;
	}
	
	public boolean saveAllVmCenterServers(List<VmCenterServer> vmCenterServers) {
		ArrayList<String> serverList = new ArrayList<String>();
		for (VmCenterServer serverCenter : vmCenterServers) {
			String serverString = vmCenterPersistenceTranslator.translateVmCenterServerToPersistence(serverCenter);
			serverList.add(serverString);
		}
		
		try {
			diskPersistence.saveVmwareCenterServers(serverList);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public boolean saveAllVmGroups(List<VmGroupBee> vmGroups) {
		ArrayList<String> vmGroupList = new ArrayList<String>();
		for (VmGroupBee vmGroup : vmGroups) {
			List<String> group = vmGroupDiskPersistenceTranslator.translateVmGroupToPersistence(vmGroup);
			vmGroupList.addAll(group);
		}
		
		try {
			diskPersistence.saveVmGroups(vmGroupList);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public List<VmGroupBee> findAllVmGroups() {
		Map<String, VmGroupBee> groupMap = new HashMap<String, VmGroupBee>();
		try {
			List<String> vmGroupStrings = diskPersistence.loadVmGroups();
			for (String string : vmGroupStrings) {
				VmGroupBee group = vmGroupDiskPersistenceTranslator.translateVmGroupToModel(string);
				if (group != null) {
					VmGroupBee vmGroupBee = groupMap.get(group.getId());
					if (vmGroupBee != null) {
						vmGroupBee.getVms().addAll(group.getVms());
					} else {
						groupMap.put(group.getId(), group);
					}
				}		
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<VmGroupBee> vmGroupBees = new ArrayList<VmGroupBee>();
		vmGroupBees.addAll(groupMap.values());		
		return vmGroupBees;
	}

}
